{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 15.12 \u5c06\u51fd\u6570\u6307\u9488\u8f6c\u6362\u4e3a\u53ef\u8c03\u7528\u5bf9\u8c61\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u5df2\u7ecf\u83b7\u5f97\u4e86\u4e00\u4e2a\u88ab\u7f16\u8bd1\u51fd\u6570\u7684\u5185\u5b58\u5730\u5740\uff0c\u60f3\u5c06\u5b83\u8f6c\u6362\u6210\u4e00\u4e2aPython\u53ef\u8c03\u7528\u5bf9\u8c61\uff0c\n\u8fd9\u6837\u7684\u8bdd\u4f60\u5c31\u53ef\u4ee5\u5c06\u5b83\u4f5c\u4e3a\u4e00\u4e2a\u6269\u5c55\u51fd\u6570\u4f7f\u7528\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "ctypes \u6a21\u5757\u53ef\u88ab\u7528\u6765\u521b\u5efa\u5305\u88c5\u4efb\u610f\u5185\u5b58\u5730\u5740\u7684Python\u53ef\u8c03\u7528\u5bf9\u8c61\u3002\n\u4e0b\u9762\u7684\u4f8b\u5b50\u6f14\u793a\u4e86\u600e\u6837\u83b7\u53d6C\u51fd\u6570\u7684\u539f\u59cb\u3001\u5e95\u5c42\u5730\u5740\uff0c\u4ee5\u53ca\u5982\u4f55\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import ctypes\nlib = ctypes.cdll.LoadLibrary(None)\n# Get the address of sin() from the C math library\naddr = ctypes.cast(lib.sin, ctypes.c_void_p).value\naddr"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Turn the address into a callable function\nfunctype = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double)\nfunc = functype(addr)\nfunc"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Call the resulting function\nfunc(2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "func(0)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u6784\u5efa\u4e00\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61\uff0c\u4f60\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2a CFUNCTYPE \u5b9e\u4f8b\u3002\nCFUNCTYPE() \u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u8fd4\u56de\u7c7b\u578b\u3002\n\u63a5\u4e0b\u6765\u7684\u53c2\u6570\u662f\u53c2\u6570\u7c7b\u578b\u3002\u4e00\u65e6\u4f60\u5b9a\u4e49\u4e86\u51fd\u6570\u7c7b\u578b\uff0c\u4f60\u5c31\u80fd\u5c06\u5b83\u5305\u88c5\u5728\u4e00\u4e2a\u6574\u578b\u5185\u5b58\u5730\u5740\u4e0a\u6765\u521b\u5efa\u4e00\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61\u4e86\u3002\n\u751f\u6210\u7684\u5bf9\u8c61\u88ab\u5f53\u505a\u666e\u901a\u7684\u53ef\u901a\u8fc7 ctypes \u8bbf\u95ee\u7684\u51fd\u6570\u6765\u4f7f\u7528\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u770b\u4e0a\u53bb\u53ef\u80fd\u6709\u70b9\u795e\u79d8\uff0c\u504f\u5e95\u5c42\u4e00\u70b9\u3002\n\u4f46\u662f\uff0c\u4f46\u662f\u5b83\u88ab\u5e7f\u6cdb\u4f7f\u7528\u4e8e\u5404\u79cd\u9ad8\u7ea7\u4ee3\u7801\u751f\u6210\u6280\u672f\u6bd4\u5982\u5373\u65f6\u7f16\u8bd1\uff0c\u5728LLVM\u51fd\u6570\u5e93\u4e2d\u53ef\u4ee5\u770b\u5230\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f8b\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 llvmpy \u6269\u5c55\u7684\u7b80\u5355\u4f8b\u5b50\uff0c\u7528\u6765\u6784\u5efa\u4e00\u4e2a\u5c0f\u7684\u805a\u96c6\u51fd\u6570\uff0c\u83b7\u53d6\u5b83\u7684\u51fd\u6570\u6307\u9488\uff0c\n\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2aPython\u53ef\u8c03\u7528\u5bf9\u8c61\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from llvm.core import Module, Function, Type, Builder\nmod = Module.new('example')\nf = Function.new(mod,Type.function(Type.double(), \\"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "block = f.append_basic_block('entry')\nbuilder = Builder.new(block)\nx2 = builder.fmul(f.args[0],f.args[0])\ny2 = builder.fmul(f.args[1],f.args[1])\nr = builder.fadd(x2,y2)\nbuilder.ret(r)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from llvm.ee import ExecutionEngine\nengine = ExecutionEngine.new(mod)\nptr = engine.get_pointer_to_function(f)\nptr"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "foo = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double, ctypes.c_double)(ptr)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Call the resulting function\nfoo(2,3)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "foo(4,5)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "foo(1,2)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5e76\u4e0d\u662f\u8bf4\u5728\u8fd9\u4e2a\u5c42\u9762\u72af\u4e86\u4efb\u4f55\u9519\u8bef\u5c31\u4f1a\u5bfc\u81f4Python\u89e3\u91ca\u5668\u6302\u6389\u3002\n\u8981\u8bb0\u5f97\u7684\u662f\u4f60\u662f\u5728\u76f4\u63a5\u8ddf\u673a\u5668\u7ea7\u522b\u7684\u5185\u5b58\u5730\u5740\u548c\u672c\u5730\u673a\u5668\u7801\u6253\u4ea4\u9053\uff0c\u800c\u4e0d\u662fPython\u51fd\u6570\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}